﻿using System;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace PSE.Framework.UI.Controls.SmartClient
{
    public class ProgressBar : System.Windows.Controls.ProgressBar
    {
        #region Construtor

        public ProgressBar()
            : base()
        {
            this.ValueChanged += new RoutedPropertyChangedEventHandler<double>(ProgressBar_ValueChanged);
            this.Loaded += new RoutedEventHandler(ProgressBar_Loaded);
        }

        void ProgressBar_Loaded(object sender, RoutedEventArgs e)
        {
            SetProgressBarValue(0);
        }

        static ProgressBar()
        {
            
        }

        #endregion

        #region Properties

        public static readonly DependencyProperty ProgressBarValueProperty = DependencyProperty.Register("ProgressBarValue", typeof(String), typeof(ProgressBar), new FrameworkPropertyMetadata("0%", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
        public String ProgressBarValue
        {
            get { return (String)GetValue(ProgressBarValueProperty); }
        }

        public static readonly DependencyProperty ProgressBarTextProperty = DependencyProperty.Register("ProgressBarText", typeof(String), typeof(ProgressBar), new FrameworkPropertyMetadata(null, CoerceProgressBarTextProperty));
        public String ProgressBarText
        {
            get { return (String)GetValue(ProgressBarTextProperty); }
            set { SetValue(ProgressBarTextProperty, value); }
        }

        public static readonly DependencyProperty ShowProgressDescriptionProperty = DependencyProperty.Register("ShowProgressDescription", typeof(Boolean), typeof(ProgressBar), new UIPropertyMetadata(false));
        public Boolean ShowProgressDescription
        {
            get { return (Boolean)GetValue(ShowProgressDescriptionProperty); }
            set { SetValue(ShowProgressDescriptionProperty, value); }
        }

        public static readonly DependencyProperty ShowMaximumProperty = DependencyProperty.Register("ShowMaximum", typeof(Boolean), typeof(ProgressBar), new UIPropertyMetadata(false));
        public Boolean ShowMaximum
        {
            get { return (Boolean)GetValue(ShowMaximumProperty); }
            set { SetValue(ShowMaximumProperty, value); }
        }

        public static readonly DependencyProperty ShowDescriptionProperty = DependencyProperty.Register("ShowDescription", typeof(Boolean), typeof(ProgressBar), new UIPropertyMetadata(false));
        public Boolean ShowDescription
        {
            get { return (Boolean)GetValue(ShowDescriptionProperty); }
        }

        public static readonly DependencyProperty UnitDescriptionProperty = DependencyProperty.Register("UnitDescription", typeof(string), typeof(ProgressBar), new UIPropertyMetadata("%"));
        public string UnitDescription
        {
            get { return (string)GetValue(UnitDescriptionProperty); }
            set { SetValue(UnitDescriptionProperty, value); }
        }
        #endregion

        #region Events
        private static object CoerceProgressBarTextProperty(DependencyObject d, object value)
        {
            ProgressBar progressBar = (ProgressBar)d;
            string valueProgressBarText = value as string;

            if(String.IsNullOrEmpty(valueProgressBarText))
                progressBar.SetValue(ShowDescriptionProperty, false);
            else
                progressBar.SetValue(ShowDescriptionProperty, true);

            return value;
        }

        void ProgressBar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            SetProgressBarValue((Int64)e.NewValue);
        }

        private void SetProgressBarValue(Int64 progressBarValue)
        {
            string progressBarValueDescription = String.Empty;

            if (!ShowMaximum)
                progressBarValueDescription = progressBarValue.ToString() + (UnitDescription != "%" ? " " : "") + UnitDescription;
            else
                progressBarValueDescription = progressBarValue.ToString() + " / " + this.Maximum.ToString() + (UnitDescription != "%" ? " " : "") + UnitDescription;

            SetValue(ProgressBarValueProperty, progressBarValueDescription);
        }

        #endregion
    }
}
